﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SemiCASE.Business;
using System.Data;

public partial class PopUp_DisplayUseCase : SemiCASEBasePage
{
    private string ProjectID
    {
        get
        {
            return Request.QueryString["ProjectID"] != null ? Request.QueryString["ProjectID"] : String.Empty;
        }
    }

    private string UseCaseID
    {
        get
        {
            return Request.QueryString["UseCaseID"] != null ? Request.QueryString["UseCaseID"] : String.Empty;
        }
    }


    private Dictionary<string, string> OrderSign
    {
        get
        {
            Dictionary<string, string> orderSign = new Dictionary<string, string>();
            orderSign.Add("1", "a");
            orderSign.Add("2", "b");
            orderSign.Add("3", "c");
            orderSign.Add("4", "d");
            orderSign.Add("5", "e");
            orderSign.Add("6", "f");
            orderSign.Add("7", "g");
            orderSign.Add("8", "h");
            orderSign.Add("9", "i");
            orderSign.Add("10", "j");
            return orderSign;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadUseCaseDetails();
        }
    }

    private void LoadUseCaseDetails()
    {
        Session["MSS"] = null;
        Session["Extentions"] = null;
        Session["ExtentionSteps"] = null;
        gvMSS.Columns[2].Visible = false;
        gvMSS.Columns[3].Visible = false;
        gvMSS.Columns[4].Visible = false;
        gvExtention.Columns[4].Visible = false;
        gvExtention.Columns[5].Visible = false;
        DropDownListPriority.Enabled = false;
        DropDownListStatus.Enabled = false;
        LoadStatus();
        LoadUseCaseInfo();
        LoadMSSFromDB();
        LoadExtentionStepsFromDB();
        LoadExtentionsFromDB();
        SetFunctionalNonFunctionalReq();
        LoadTestCases();
        LoadWorkItems();
    }

    private void LoadWorkItems()
    {
        DataTable dt = new WorkItemBS().LoadWorkItemFromDB(new Guid(this.ProjectID), new Guid(this.UseCaseID));

        if (dt == null || dt.Rows.Count == 0)
        {
            gvWorkItem.DataSource = null;
            gvWorkItem.DataBind();
        }
        else
        {
            gvWorkItem.DataSource = dt;
            gvWorkItem.DataBind();
        }
    }

    private void LoadTestCases()
    {
        DataTable dt = new TestCaseBS().LoadTestCaseFromDB(new Guid(this.ProjectID), new Guid(this.UseCaseID));

        if (dt == null || dt.Rows.Count == 0)
        {
            gvTestCase.DataSource = null;
            gvTestCase.DataBind();
        }
        else
        {
            gvTestCase.DataSource = dt;
            gvTestCase.DataBind();
        }
    }

    private void SetFunctionalNonFunctionalReq()
    {
        DataTable dt = (DataTable)Session["UseCaseList"];
        if (dt == null || dt.Rows.Count == 0)
            return;

        foreach (DataRow row in dt.Rows)
        {
            if (row["UseCaseID"].ToString() == this.UseCaseID)
            {
                if (row["Nonfunctional"] != DBNull.Value)
                {
                    if (Convert.ToBoolean(row["Nonfunctional"]))
                    {
                        RadioButtonNonFunctional.Checked = true;
                        RadioButtonFunctionalReq.Checked = false;
                        TableFunctional.Visible = false;
                    }
                    else
                    {
                        RadioButtonFunctionalReq.Checked = true;
                        RadioButtonNonFunctional.Checked = false;
                        TableFunctional.Visible = true;
                    }
                }
            }
        }
    }

    private void LoadUseCaseInfo()
    {
        DataTable dt = (DataTable)Session["UseCaseList"];
        if (dt == null || dt.Rows.Count == 0)
            return;

        foreach (DataRow row in dt.Rows)
        {
            if (row["UseCaseID"].ToString() == this.UseCaseID)
            {
                GenelTextBoxFrequency.Text = row["Frequency"] != DBNull.Value ? row["Frequency"].ToString() : String.Empty;
                GenelTextBoxUseCaseName.Text = row["Name"] != DBNull.Value ? row["Name"].ToString() : String.Empty;
                GenelTextBoxScope.Text = row["Scope"] != DBNull.Value ? row["Scope"].ToString() : String.Empty;
                GenelTextBoxLevel.Text = row["Level"] != DBNull.Value ? row["Level"].ToString() : String.Empty;
                GenelTextBoxPrimaryActor.Text = row["PrimaryActor"] != DBNull.Value ? row["PrimaryActor"].ToString() : String.Empty;
                GenelTextBoxStakeHolders.Text = row["StakeHolders"] != DBNull.Value ? row["StakeHolders"].ToString() : String.Empty;
                GenelTextBoxPreconditions.Text = row["Precondition"] != DBNull.Value ? row["Precondition"].ToString() : String.Empty;
                GenelTextBoxSuccessGuarantees.Text = row["SuccessGuarantee"] != DBNull.Value ? row["SuccessGuarantee"].ToString() : String.Empty;
                GenelTextBoxSpecialRequirements.Text = row["SpecialRequirement"] != DBNull.Value ? row["SpecialRequirement"].ToString() : String.Empty;
                GenelTextBoxTechnology.Text = row["Technology"] != DBNull.Value ? row["Technology"].ToString() : String.Empty;
                GenelTextBoxMisc.Text = row["Miscelleneous"] != DBNull.Value ? row["Miscelleneous"].ToString() : String.Empty;
                if (row["StatusID"] != DBNull.Value)
                    (DropDownListStatus.Items.FindByValue(row["StatusID"].ToString())).Selected = true;
                if (row["Priority"] != DBNull.Value)
                    (DropDownListPriority.Items.FindByValue(row["Priority"].ToString())).Selected = true;
                break;
            }
        }
    }

    private void LoadMSSFromDB()
    {
        string p = this.ProjectID;
        string u = this.UseCaseID;
        if (String.IsNullOrEmpty(p) || String.IsNullOrEmpty(u))
            return;

        DataTable dt = new MainSuccessBS().LoadMSSFromDB(new Guid(p), new Guid(u));
        Session["MSS"] = dt;
        LoadMSS();
    }

    private void LoadExtentionStepsFromDB()
    {
        string p = this.ProjectID;
        string u = this.UseCaseID;
        if (String.IsNullOrEmpty(p) || String.IsNullOrEmpty(u))
            return;

        DataTable dt = new ExtentionStepBS().LoadExtentionStepsFromDB(new Guid(p), new Guid(u));
        Session["ExtentionSteps"] = dt;
        LoadExtentions();
    }

    private void LoadExtentionsFromDB()
    {
        string p = this.ProjectID;
        string u = this.UseCaseID;
        if (String.IsNullOrEmpty(p) || String.IsNullOrEmpty(u))
            return;

        DataTable dt = new ExtentionBS().LoadExtentionsFromDB(new Guid(p), new Guid(u));
        Session["Extentions"] = dt;
        LoadExtentions();
    }

    private void LoadStatus()
    {
        DataTable dt = new UseCaseBS().LoadStatus();
        if (dt.Rows.Count > 0)
        {
            DropDownListStatus.DataSource = dt;
            DropDownListStatus.DataBind();
        }
        else
        {
            DropDownListStatus.DataSource = null;
            DropDownListStatus.DataBind();
        }
    }

    private void LoadExtentions()
    {
        DataTable dt = (DataTable)Session["Extentions"];
        if (dt == null)
        {
            gvExtention.DataSource = null;
            gvExtention.DataBind();
        }
        else
        {
            if (dt.Rows.Count > 0)
            {
                dt.DefaultView.Sort = "MssSiraNo ASC, OrderSign ASC";
                gvExtention.DataSource = dt.DefaultView.ToTable();
                gvExtention.DataBind();
            }
            else
            {
                gvExtention.DataSource = null;
                gvExtention.DataBind();
            }
        }
    }

    private void LoadMSS()
    {
        DataTable dt = (DataTable)Session["MSS"];
        if (dt == null)
        {
            gvMSS.DataSource = null;
            gvMSS.DataBind();
        }
        else
        {
            if (dt.Rows.Count > 0)
            {
                dt.DefaultView.Sort = "SiraNo ASC";
                gvMSS.DataSource = dt.DefaultView.ToTable();
                gvMSS.DataBind();
                Session["MSS"] = dt.DefaultView.ToTable();
            }
            else
            {
                gvMSS.DataSource = null;
                gvMSS.DataBind();

            }
        }
    }

    private void DownMss(int pSiraNo)
    {
        DataTable dt = (DataTable)Session["MSS"];
        if (pSiraNo == dt.Rows.Count)
            return;

        dt.Rows[pSiraNo - 1]["SiraNo"] = pSiraNo + 1;
        dt.Rows[pSiraNo]["SiraNo"] = pSiraNo;
        Session["MSS"] = dt;
        SetExtentionSiraNo();
        LoadMSS();
    }

    private void UpMss(int pSiraNo)
    {
        DataTable dt = (DataTable)Session["MSS"];
        if (pSiraNo == 1)
            return;

        dt.Rows[pSiraNo - 1]["SiraNo"] = pSiraNo - 1;
        dt.Rows[pSiraNo - 2]["SiraNo"] = pSiraNo;
        Session["MSS"] = dt;
        SetExtentionSiraNo();
        LoadMSS();
    }

    private void SetExtentionSiraNo()
    {
        DataTable dt = (DataTable)Session["MSS"];
        DataTable dtExt = (DataTable)Session["Extentions"];

        if (dt == null || dtExt == null)
            return;

        for (int k = 0; k < dt.Rows.Count; k++)
        {
            if (dtExt != null)
            {
                for (int m = 0; m < dtExt.Rows.Count; m++)
                {
                    if (dt.Rows[k]["MssID"].ToString() == dtExt.Rows[m]["MssID"].ToString())
                        dtExt.Rows[m]["MssSiraNo"] = dt.Rows[k]["SiraNo"].ToString();
                }
            }
        }
        Session["Extentions"] = dtExt;
        LoadExtentions();
    }
}